// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import BaseView from "../base/BaseView";
import PlateListComponent from "../base/components/PlateListComponent";
import Top10HoldersComponent from "../base/components/Top10HoldersComponent";
import TushareModule from "../module/TushareModule";

const {ccclass, property} = cc._decorator;

@ccclass
export default class StockInfoView extends BaseView {

    @property(cc.Label)
    labelInfo: cc.Label = null;

    @property(cc.Label)
    labelName: cc.Label = null;

    @property(cc.Node)
    content: cc.Node = null;

    @property(cc.Prefab)
    plateListPrefab: cc.Prefab = null;

    @property(cc.Prefab)
    top10HoldersPrefab: cc.Prefab = null;

    private _selectIndex: number = 0;

    private _stocks: any = null;

    industryComp: PlateListComponent = null;

    conceptComp: PlateListComponent = null;

    top10Comp: Top10HoldersComponent = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        let industryLayer = cc.instantiate(this.plateListPrefab);
        this.content.addChild(industryLayer);
        this.industryComp = industryLayer.getComponent(PlateListComponent);
        let conceptLayer = cc.instantiate(this.plateListPrefab);
        this.content.addChild(conceptLayer);
        this.conceptComp = conceptLayer.getComponent(PlateListComponent);
        let top10Layer = cc.instantiate(this.top10HoldersPrefab);
        this.content.addChild(top10Layer);
        this.top10Comp = top10Layer.getComponent(Top10HoldersComponent);
    }

    protected onEnable(): void {
        this.node.on(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheel, this, true);
    }

    protected onDisable(): void {
        this.node.off(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheel, this, true);
    }

    onMouseWheel (event) {
        let y = event.getScrollY();
        if (y > 0) {
            this.onBtnUp();
        } else {
            this.onBtnDown();
        }
    }

    onBtnDown () {
        this._selectIndex++;
        if (this._selectIndex >= this._stocks.length) {
            this._selectIndex = this._stocks.length - 1;
        }
        this.updateView();
    }

    onBtnUp () {
        this._selectIndex--;
        if (this._selectIndex < 0) {
            this._selectIndex = 0;
        }
        this.updateView();
    }

    onBtnLookKline () {
        let params = {stocks: this._stocks, selectIndedx: this._selectIndex};
        this.createDialog(BaseView.Views.StockKLineView, params);
    }

    show (params) {
        super.show(params);
        this._selectIndex = params.selectIndex;
        this._stocks = params.stocks;
        this.updateView();
    }

    async updateView () {
        let stock = this._stocks[this._selectIndex];
        if (stock) {
            let ts_code = stock.ts_code;
            let detail = TushareModule.getInstance().getStockDetail(ts_code);
            let info = detail.info;
            let market = detail.market;
            this.labelName.string = info.name;
            let strInfo = `${info.name}  ${info.ts_code}  行业:${info.industry}  市盈率:${market.pe}  市盈率ttm:${market.pe_ttm} \n`
            strInfo += `上市时间:${info.list_date}  市净率:${market.pb}  市值:${Math.floor(market.total_mv / 100)/ 100}亿  流通市值:${Math.floor(market.circ_mv / 100)/ 100}亿 `;
            this.labelInfo.string = strInfo;

            let plate = detail.plate; 
            let industrys = plate.industry.concat(plate.subtrade);
            this.industryComp.setData(industrys, '行业', false);
            this.conceptComp.setData(plate.concept, '概念', false);
            let top10Data = await TushareModule.getInstance().getTop10Holders(ts_code);
            this.top10Comp.setData(top10Data);
        }
    }

}
